package com.ljx.rabbit.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ljx.rabbit.context.BaseContext;
import com.ljx.rabbit.domain.dto.AddressDTO;
import com.ljx.rabbit.domain.po.Address;
import com.ljx.rabbit.domain.po.Districts;
import com.ljx.rabbit.result.R;
import com.ljx.rabbit.service.IAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@Api(tags = "地址")
@Slf4j
@RequestMapping("/member/address")
@CrossOrigin
public class AddressController {
    private final IAddressService addressService;

    @ApiOperation(value = "新增地址，获取地址列表")
    @GetMapping("/getArea")
    public R<List<Districts>> getArea(@RequestParam(required = false) String keywords) {
        List<Districts> districts = addressService.getArea(keywords);
        return R.success(districts);
    }

    @ApiOperation(value = "新增地址")
    @PostMapping
    public R<String> addAddress(@RequestBody AddressDTO addressDTO) {
        QueryWrapper<Address> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(Address::getUserId, BaseContext.getCurrentId())
                .eq(Address::getIsDefault, 0);
        Address address = new Address();
        if(addressService.getOne(wrapper)==null){//是否有默认地址
            //无默认
            address.setIsDefault(0);
        }

        BeanUtils.copyProperties(addressDTO, address);
        address.setUserId(BaseContext.getCurrentId());
        addressService.saveOrUpdate(address);
        return R.success("新增地址成功");
    }

    @ApiOperation(value = "查询地址")
    @GetMapping
    public R<List<Address>> getAddress() {
        List<Address> list = addressService.listAddress();
        return R.success(list);
    }

    @ApiOperation(value = "删除地址")
    @DeleteMapping("/{id}")
    public R<String> deleteAddress(@PathVariable Long id) {
        try {
            addressService.removeById(id);
        } catch (Exception e) {
//            throw new RuntimeException("删除失败，有订单正在使用这个地址");
            return R.error("删除失败，有订单正在使用这个地址");
        }
        log.info("删除地址：{}", id);
        return R.success("删除地址成功");
    }

    @ApiOperation(value = "修改地址")
    @PutMapping
    public R<String> updateAddress(@RequestBody AddressDTO addressDTO) {
        Address address = new Address();
        BeanUtils.copyProperties(addressDTO, address);
        addressService.updateById(address);
        return R.success("修改地址成功");
    }

    @ApiOperation(value = "设置默认地址")
    @PutMapping("/setDefault")
    public R<String> setDefault(@RequestParam Long id) {
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", BaseContext.getCurrentId());
        List<Address> list = addressService.list(queryWrapper);
        for (Address address : list) {
            address.setIsDefault(1);
            addressService.updateById(address);
        }
        Address address = new Address();
        address.setId(id);
        address.setIsDefault(0);
        addressService.updateById(address);
        return R.success("设置成功");
    }
}
